package com.signway.swmediaplayer.utils

import android.annotation.SuppressLint
import java.lang.Exception
import java.lang.reflect.Method

object SystemProperties {

    @JvmStatic
    operator fun get(key: String?): String {
        init()
        var value: String = ""
        try {
            value = mGetMethod!!.invoke(mClassType, key) as String
        } catch (e: Exception) {
            e.printStackTrace()
        }
        return value
    }

    @JvmStatic
    fun getInt(key: String?, def: Int): Int {
        init()
        var value = def
        try {
            val v = mGetIntMethod!!.invoke(mClassType, key, def) as Int
            value = v
        } catch (e: Exception) {
            e.printStackTrace()
        }
        return value
    }

    @JvmStatic
    fun getBoolean(key: String?, def: Boolean): Boolean {
        init()
        var value = def
        try {
            val v = mGetBooleanMethod!!.invoke(mClassType, key, def) as Boolean
            value = v
        } catch (e: Exception) {
            e.printStackTrace()
        }
        return value
    }

    val sdkVersion: Int
        get() = getInt("ro.build.version.sdk", -1)

    //-------------------------------------------------------------------
    private var mClassType: Class<*>? = null
    private var mGetMethod: Method? = null
    private val mSetMethod: Method? = null
    private var mGetIntMethod: Method? = null
    private var mGetBooleanMethod: Method? = null
    @SuppressLint("PrivateApi")
    private fun init() {
        try {
            if (mClassType == null) {
                mClassType = Class.forName("android.os.SystemProperties")
                mGetMethod = mClassType?.getDeclaredMethod("get", String::class.java)
                mGetIntMethod = mClassType?.getDeclaredMethod(
                    "getInt",
                    String::class.java,
                    Int::class.javaPrimitiveType
                )
                mGetBooleanMethod = mClassType?.getDeclaredMethod(
                    "getBoolean",
                    String::class.java,
                    Boolean::class.javaPrimitiveType
                )
            }
        } catch (e: Exception) {
            e.printStackTrace()
        }
    }

    @SuppressLint("PrivateApi")
    @JvmStatic
    fun setProp(key: String?, value: String?) {
        try {
            val c = Class.forName("android.os.SystemProperties")
            val set = c.getMethod("set", String::class.java, String::class.java)
            set.invoke(c, key, value)
        } catch (e: Exception) {
            e.printStackTrace()
        }
    }

    @SuppressLint("PrivateApi")
    fun getProp(key: String?, defaultValue: String?): String? {
        var value = defaultValue
        try {
            val c = Class.forName("android.os.SystemProperties")
            val get = c.getMethod("get", String::class.java, String::class.java)
            value = get.invoke(c, key, "unknown") as String
        } catch (e: Exception) {
            e.printStackTrace()
        }
        return value
    }
}